﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Utils;
using BitPortal.Business;
using BitPortal.Models;

namespace BitPortal.Admin.Media.Media
{
    public partial class Default : Controls.BaseAdminPage
    {
        protected override void OnInit(EventArgs e)
        {
            PermissionBLL.CheckRoleThrowException(role: "MediaView", useExceptionHandler: true);
            base.OnInit(e);
            (this.MediaAddEdit1 as Controls.BaseAdminControl).ReloadControls.Add(this.GridPanel1);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["AddToGallery"]))
            {
                btnAddToGallery.Hidden = false;
            }
            if (!string.IsNullOrEmpty(Request.QueryString["AddToProduct"]))
            {
                btnAddToProduct.Hidden = false;
            }
        }
        public void LoadGridData()
        {
            (this.Master as AdminFunction).MasterResourceManager.AddScript("Ext.getCmp('" + GridPanel1.ClientID + "').getStore().reload();");
            (this.GridPanel1.SelectionModel.Primary as RowSelectionModel).ClearSelections();
        }
        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            FilterConditions fc = null;
            string s = e.Parameters[this.GridFilters1.ParamPrefix];
            if (!string.IsNullOrEmpty(s))
            {
                fc = new FilterConditions(s);
            }
            this.Store1.DataSource = ExtNet.GirdData.Filter<Models.Media>((new ExtendedBitPortalEntities()).Medias.ToList(), fc, e);
        }

        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            MediaAddEdit1.BindData(null);
        }

        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            if (sm.SelectedRows.Count > 0)
            {
                MediaAddEdit1.BindData(Convert.ToInt64(sm.SelectedRows[0].RecordID));
            }
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("MediaDelete");
                int record = 0;
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    if (BitPortal.Business.MediaBLL.Delete(Convert.ToInt64(row.RecordID)) > 0)
                    {
                        record++;
                    }
                }
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " media thành công!");
                    LoadGridData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnAddToGallery_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("MediaEdit");
                if (!string.IsNullOrEmpty(Request.QueryString["AddToGallery"]))
                {
                    int GalleryId = Utils.GeneralUtils.ToInt32TryParse(Request.QueryString["AddToGallery"]);
                    if (GalleryId > 0)
                    {
                        int record = 0;
                        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                        foreach (SelectedRow row in sm.SelectedRows)
                        {
                            try
                            {
                                if (Business.GalleryBLL.AddMedia(GalleryId, Convert.ToInt64(row.RecordID)) > 0)
                                {
                                    record++;
                                }
                            }
                            catch (Exception ex)
                            {
                                ShowNotification(message: ex.Message, icon: Icon.Error);
                            }
                        }
                        ShowNotification("Thêm " + record + " media vào gallery thành công!");
                    }
                }
                //Close window
                AdminFunction m = (AdminFunction)this.Master;
                m.MasterResourceManager.AddScript("parent.Ext.WindowMgr.getActive().hide();");
                m.MasterResourceManager.AddScript("parent.Ext.getCmp('" + Request.QueryString["ReloadID"] + "').getStore().reload();");
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnAddToProduct_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ProductEdit");

                if (!string.IsNullOrEmpty(Request.QueryString["AddToProduct"]))
                {
                    long ProductId = Request.QueryString["AddToProduct"].ToInt64TryParse();
                    if (ProductId > 0)
                    {
                        int record = 0;
                        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                        foreach (SelectedRow row in sm.SelectedRows)
                        {
                            try
                            {
                                if (Business.ProductBLL.AddMedia(ProductId, Convert.ToInt64(row.RecordID)) > 0)
                                {
                                    record++;
                                }
                            }
                            catch (Exception ex)
                            {
                                ShowNotification(message: ex.Message, icon: Icon.Error);
                            }
                        }
                        ShowNotification("Thêm " + record + " media vào Product thành công!");
                    }
                }

                //Close window
                AdminFunction m = (AdminFunction)this.Master;
                m.MasterResourceManager.AddScript("parent.Ext.WindowMgr.getActive().hide();");
                m.MasterResourceManager.AddScript("parent.Ext.getCmp('" + Request.QueryString["ReloadID"] + "').getStore().reload();");
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

    }
}